package com.yr.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yr.common.util.PageResult;
import com.yr.entity.Classes;
import com.yr.entity.Student;
import com.yr.mapper.StudentMapper;
import com.yr.service.IClassesService;
import com.yr.service.IStudentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yr.vo.studentvo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yr
 * @since 2025-01-07
 */
@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements IStudentService {
    @Autowired
    private IClassesService classesService;
    @Override
    public PageResult<Student> findbyPage(Integer page, Integer pagesize) {
        Page<Student> pagestu = this.page(new Page<>(page, pagesize));
        return new PageResult<>(pagestu.getRecords(),pagestu.getTotal());
    }

//    @Override
//    public PageResult<Student> search(Integer page, Integer pagesize) {
//        return null;
//    }

    @Override
    public PageResult<Student> search(Integer page, Integer pagesize, studentvo vo) {
        // 1. 进行分页带条件查询
        // 1.1 封装查询条件
        QueryWrapper<Student> wrapper = new QueryWrapper<>();
        wrapper.like(StrUtil.isNotBlank(vo.getStudentname()), "studentname", vo.getStudentname());
        wrapper.like(StrUtil.isNotBlank(vo.getStudentddr()), "studentddrddr", vo.getStudentddr());
        wrapper.eq(vo.getStudentid() != null && vo.getStudentid() != 0, "studentid", vo.getStudentid());
        // 1.2 开始分页查询
        Page<Student> page1 = this.page(new Page<>(page, pagesize), wrapper);
        // 1.3 处理学生所在班级名称
        page1.getRecords().forEach(st -> {
            Classes classes = classesService.getById(st.getStudentid());
            st.setStudentname(classes.getClassname());
        });
        // 2. 封装分页结果
        return new PageResult<>(page1.getRecords(),page1.getTotal());
    }

    @Override
    public boolean add(Student student) {
        return this.save(student);
    }
}
